home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: callback function in a class
- Date: 16 Feb 1996 11:16:20 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4g1p24$lpi@news1.usa.pipeline.com>
- References: <Pine.OSF.3.91.960214160359.19924A-100000@stio1>
- NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Feb 14, 1996 16:09:30 in article <callback function in a class>,
- 'Schmidt Carsten <i010@stio1>' wrote:
-
-
- >
- >Hi, everybody,
- >
- >Does anybody have an idea, how I can put a callback function in a class,
- >look for the example:
- >
- >class X {
- >DWORD ThreadProc (..parms...);
- >void StartIt (void);
- >....
- >};
- >
- >DWORD X::ThreadProc (...parms...)
- >{
- >....
- >}
- >
- >void X::StartIt (void)
- >{
- >hThread = CreateThread (..., ThreadProc, ...);
- >}
- >
- >Don't worry about Threads, it is just an example. I try to find a
- >solution for the general problem: How to use a callback-function in a
- class.
- >
- There's no way to implement a callback function as a nonstatic
- C++ member directly. It is, however, easy to do it indirectly.
-
- void callback (void * p)
- {
- ((X*)p)->ThreadProc();
- }
-
- then you can:
- hThread = CreateThread (..., callback, (void*)this, ...);
-
- Of course, you are now responsible for ensuring that the pointer
- passed to callback is compatible with the type to which it's
- being casted. Also, if you like, callback canb e implemented
- as a static member function of X.
-
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-